package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.AdvisoryDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 咨询业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-08-13 13:43:14
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class AdvisoryService{

  @Resource
  private AdvisoryDao advisoryDao;

  public String editDisplay(final HttpServletRequest request){
    final PageFormData formData = new PageFormData(request);
    final String p_kid = "kid";
    final String p_disable = "disable";
    final String p_display = "display";
    final String validate = ToolClient.validateField(formData,p_disable,p_display,p_kid);
    if(validate != null)return validate;
    final String fieldInteger = ToolClient.validateInteger(formData,p_disable,p_display);
    if(fieldInteger != null)return fieldInteger;
    final String exist_key = advisoryDao.queryExistById(formData.getString(p_kid));
    if(exist_key == null){
      return ToolClient.createJsonFail("数据不存在,刷新重试");
    }
    return ToolClient.executeRows(advisoryDao.editDisplay(formData));
  }

  public String queryById(final PageFormData formData){
    final String p_id = "id";
    final String validate = ToolClient.validateField(formData,p_id);
    if(validate != null)return validate;
    return ToolClient.queryJson(advisoryDao.queryById(formData.getString(p_id)));
  }

  public String delById(final PageFormData formData){
    final String p_kid = "id";
    final String validate = ToolClient.validateField(formData,p_kid);
    if(validate != null)return validate;
    final String kid = formData.getString(p_kid);
    final String exist_key = advisoryDao.queryExistById(kid);
    if(exist_key == null){
      return ToolClient.createJsonFail("删除失败,数据已不存在");
    }
    return ToolClient.executeRows(advisoryDao.delById(kid));
  }

  public String listData(PageFormData formData){
    try {
      formData = ToolClient.dataTableMysql(formData);
      if(formData == null)return ToolClient.jsonValidateField();
      final HashMap<String,Object> map = advisoryDao.listData(formData);
      return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
    } catch (Exception e){
      return ToolClient.dataTableException(formData.get("sEcho"));
    }
  }

  public String getSkilledDomain(){
    return ToolClient.queryJson(advisoryDao.getSkilledDomain());
  }
}